package cn.bustack.middleware.dynamic.thread.pool.sdk.domain;

import cn.bustack.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

/*
 * 动态线程池服务实现类
 * 作为一个bean存在，在项目启动的时候就自动拿到了所有属性
 * */
public class DynamicThreadPoolSercive implements IDynamicThreadPoolService{

    //日志
    private final Logger logger = LoggerFactory.getLogger(DynamicThreadPoolSercive.class);

    //线程池信息，用构造方法传
    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;

    //应用名字，用构造方法传
    private final String applicationName;

    public DynamicThreadPoolSercive( String applicationName,Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.threadPoolExecutorMap = threadPoolExecutorMap;
        this.applicationName = applicationName;
    }

    /*
    * 查询所有线程池
    * */
    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolList() {
        //拿到所有的线程池名字
        Set<String> threadPoolBeanNames = threadPoolExecutorMap.keySet();
        //创建一个list集合，存放所有线程池
        List<ThreadPoolConfigEntity> threadPoolVOS = new ArrayList<>(threadPoolBeanNames.size());
        //通过循环，封装每个线程池对象并加入到集合中
        for (String beanName : threadPoolBeanNames) {
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(beanName);
            ThreadPoolConfigEntity threadPoolConfigVO = new ThreadPoolConfigEntity(applicationName, beanName);
            threadPoolConfigVO.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            threadPoolConfigVO.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
            threadPoolConfigVO.setActiveCount(threadPoolExecutor.getActiveCount());
            threadPoolConfigVO.setPoolSize(threadPoolExecutor.getPoolSize());
            threadPoolConfigVO.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
            threadPoolConfigVO.setQueueSize(threadPoolExecutor.getQueue().size());
            threadPoolConfigVO.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
            threadPoolVOS.add(threadPoolConfigVO);
        }
        //将所有拿到的线程池返回
        return threadPoolVOS;
    }

    /*
    * 用指定名字的方式去获得指定的线程池
    * threadPoolName：传过来的名字
    * */
    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName) {
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        if(threadPoolExecutor == null){
            //如果传过来的名字为空，返回一个空就行了
            return new ThreadPoolConfigEntity(applicationName,threadPoolName);
        }
        //确实有这个名字的线程池，生成这个线程池的一个对象
        ThreadPoolConfigEntity threadPoolConfigVO = new ThreadPoolConfigEntity(applicationName, threadPoolName);
        threadPoolConfigVO.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        threadPoolConfigVO.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        threadPoolConfigVO.setActiveCount(threadPoolExecutor.getActiveCount());
        threadPoolConfigVO.setPoolSize(threadPoolExecutor.getPoolSize());
        threadPoolConfigVO.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        threadPoolConfigVO.setQueueSize(threadPoolExecutor.getQueue().size());
        threadPoolConfigVO.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());

        if(logger.isDebugEnabled()){
            //如果是调试模式，打印
            logger.info("动态线程池，配置查询 应用名：{} 线程名：{} 池化配置：{}", applicationName,threadPoolName, JSON.toJSONString(threadPoolConfigVO));
        }
        //返回这个线程池对象
        return threadPoolConfigVO;
    }

    /*
    * 更新指定线程池信息
    * */
    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        //如果这个线程名字为空，返回空
        if(threadPoolConfigEntity == null || !applicationName.equals(threadPoolConfigEntity.getAppName())){
            return;
        }
        //如果没有这个线程，返回
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        if(threadPoolExecutor == null){
            return;
        }
        //进行到这个说明找到了这个线程threadPoolExecutor
        // 设置参数 「调整核心线程数和最大线程数」
        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());

    }
}
